package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingMapper;
import com.sky.service.ShoppingService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingServiceImpl implements ShoppingService {
    @Autowired
    private ShoppingMapper shoppingMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加商品进购物车
     * 思路：
     * 1.判断购物车中是否已经存在该商品
     *      如果存在数量+1
     * 2.如果不存在 判断是dish 还是 setmeal
     *
     * 3.从dish/setmeal中获取信息填充购物车表中的冗余字段
     *
     * 4.插入数据
     *
     * @param shoppingCartDTO
     */
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //只能查询自己的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //判断商品当前是否在购物车
       List<ShoppingCart> list= shoppingMapper.list(shoppingCart);
       //如果存在 数量加一
       if (list!=null&&list.size()>0){
            shoppingCart = list.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
            shoppingMapper.updateNumById(shoppingCart);
       }else {
           //如果不存在 判断是菜品还是套餐 插入数量 1
           Long dishId = shoppingCartDTO.getDishId();
           //如果添加到是菜品
           if (dishId!=null){
               //给购物车表中的冗余字段赋值 减少联查
               Dish dish = dishMapper.select(shoppingCartDTO.getDishId());
               shoppingCart.setName(dish.getName());
               shoppingCart.setImage(dish.getImage());
               shoppingCart.setAmount(dish.getPrice());
           }else {
               //添加的是套餐
               Setmeal setmeal = setmealMapper.quereyById(shoppingCartDTO.getSetmealId());
               shoppingCart.setName(setmeal.getName());
               shoppingCart.setAmount(setmeal.getPrice());
               shoppingCart.setImage(setmeal.getImage());
           }
           shoppingCart.setNumber(1);
           shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingMapper.insert(shoppingCart);
       }

    }

    /**
     * 查看购物车
     * @return
     */
    public List<ShoppingCart> list() {
/*        Long currentId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setId(BaseContext.getCurrentId());*/

        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();
        List<ShoppingCart> list = shoppingMapper.list(shoppingCart);
        return list;
    }

    /**
     * 清空购物车
     */
    public void clean() {
        Long currentId = BaseContext.getCurrentId();
        shoppingMapper.deleteShoppingCartById(currentId);
    }

    /**
     * 删除购物车中的商品
     * 1.查询出商品信息
     * 2.判断商品数量
     * 3.如果商品数量大于一 则数量减一
     * 4.如果商品数量为一 则删除商品
     * @param shoppingCart
     */
    public void sub(ShoppingCart shoppingCart) {
        List<ShoppingCart> list = shoppingMapper.list(shoppingCart);
        ShoppingCart shoppingCart1 = list.get(0);
        if (shoppingCart1.getNumber()>1){
            shoppingCart1.setNumber(shoppingCart1.getNumber()-1);
            shoppingMapper.updateNumById(shoppingCart1);
        }else {
            shoppingMapper.deleteById(shoppingCart1.getId());
        }
    }
}
